package com.example.fw;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.ContactData;
import com.example.tests.GroupData;
import com.example.utils.SortedListOf;

public class ContactHelper extends WebDriverHelperBase{
	
	public static boolean CREATION = true;
	public static boolean MODIFICATION = false;

	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}
	
	private SortedListOf<ContactData> cachedContacts;
	
	public SortedListOf<ContactData> getContacts() {
		if (cachedContacts == null) {
			rebuildCache();
		}
		return cachedContacts;		
	}
	
	public ContactData getContactByIndex(int index) {
		return cachedContacts.get(index);		
	}
	
	private void oldRebuildCache() {
		cachedContacts = new SortedListOf<ContactData>();
		
		manager.navigateTo().mainPage();		
		List<WebElement> checkboxes = driver.findElements(By.name("selected[]"));
		for (WebElement checkbox : checkboxes) {
			String title = checkbox.getAttribute("title");
			title = title.substring("Select (".length(), title.length() - ")".length());
			cachedContacts.add(new ContactData()
				.withLastName(title.substring(title.indexOf(" ") + 1)));			
		}		
	}
	
	private void rebuildCache() {
		cachedContacts = new SortedListOf<ContactData>(
				manager.getHibernateHelper().listContacts());			
	}
	
	public ContactHelper createContact(ContactData contact) {
		manager.navigateTo().mainPage();
		initContactCreation();
    	fillContactForm(contact, CREATION);
    	submitContactCreation();
    	returnToHomePage();
    	rebuildCache();
    	return this;
	}
	
	public ContactHelper modifyContact(int index, ContactData contact) {
		initContactModification(index);
    	completeContactModification(contact);
    	return this;
	}
	
	public ContactHelper completeContactModification(ContactData contact) {
		fillContactForm(contact, MODIFICATION);
    	submitContactModification();
    	returnToHomePage();	
    	rebuildCache();
    	return this;
	}

	public ContactHelper deleteContact(int index) {
		initContactModification(index);
		submitContactDeletion();
		returnToHomePage();
		rebuildCache();
		return this;
	}

	// -------------------------------------------------------------------------

	public SortedListOf<ContactData> getUiContacts() {
		SortedListOf<ContactData> contacts = new SortedListOf<ContactData>();
		
		manager.navigateTo().mainPage();		
		List<WebElement> checkboxes = driver.findElements(By.name("selected[]"));
		for (WebElement checkbox : checkboxes) {
			String title = checkbox.getAttribute("title");
			title = title.substring("Select (".length(), title.length() - ")".length());
			contacts.add(new ContactData()
				.withLastName(title.substring(title.indexOf(" ") + 1)));			
		}	
		return contacts;
	}
	
	public ContactData getCurrentUiContact() {
		ContactData contact = new ContactData()
			.withLastName(getFieldValue("lastname"));
		return contact;
	}

	private String getFieldValue(String fieldName) {
		WebElement field = driver.findElement(By.name(fieldName));
		String value = field.getAttribute("value");
		return value;
	}
	
	public ContactHelper returnToHomePage() {
		click(By.linkText("home"));
		return this;
	}
	
	public ContactHelper fillContactForm(ContactData contact, boolean formType) {
		type(By.name("firstname"), contact.getFirstName());
	    type(By.name("lastname"), contact.getLastName());
	    type(By.name("address"), contact.getAddress());
	    type(By.name("home"), contact.getHomePhone());
	    type(By.name("mobile"), contact.getMobile());
	    type(By.name("work"), contact.getWorkPhone());
	    type(By.name("email"), contact.getEmail());
	    type(By.name("email2"), contact.getEmail2());
	    selectByText(By.name("bday"), contact.getBday());
	    selectByText(By.name("bmonth"), contact.getBmonth());
	    type(By.name("byear"), contact.getByear());
	    if (formType == CREATION) {
	    	selectByText(By.name("new_group"), contact.getGroup());
	    } else {
	    	if (driver.findElements(By.name("new_group")).size() != 0) {
	    		throw new Error("Group selector exists in contact modification form");
	    	}
	    }
	    type(By.name("address2"), contact.getAddress2());
	    type(By.name("phone2"), contact.getPhone2());
	    return this;
	  }

	public ContactHelper submitContactCreation() {
		click(By.name("submit"));
		cachedContacts = null;
		return this;
	  }

	public ContactHelper initContactCreation() {
		click(By.linkText("add new"));
		return this;
	  }

	private void selectContactByIndex(int index) {
		click(By.xpath("(//img[@alt='Edit'])[" + (index+1) + "]"));		
	}

	public ContactHelper initContactModification(int index) {
		selectContactByIndex(index);
		return this;
	}

	public ContactHelper submitContactModification() {
		click(By.name("update"));
		cachedContacts = null;
		return this;
	}	
	
	public ContactHelper submitContactDeletion() {
		click(By.xpath("(//input[@name='update'])[2]"));
		cachedContacts = null;
		return this;
	}
}
